<!DOCTYPE html>
<html lang=zh>
<head>
    <meta charset="utf-8">
    
    <title>第五十二章：基于SpringBoot2使用Rest访问MongoDB数据 | 恒宇少年De成长之路</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="在之前项目中我们想要读取MongoDB内的内容需要使用MongoDBTemplate来完成数据的CRUD，那如果我们想要通过RestController的形式获取MongoDB内的数据就更麻烦了，还需要自行去创建对应的控制器，然后使用MongoDBTemplate从MongoDB内读取出数据后返回给前端。">
<meta name="keywords" content="SpringBoot">
<meta property="og:type" content="article">
<meta property="og:title" content="第五十二章：基于SpringBoot2使用Rest访问MongoDB数据">
<meta property="og:url" content="http://blog.yuqiyu.com/spring-boot-chapter-51的副本/index.html">
<meta property="og:site_name" content="恒宇少年De成长之路">
<meta property="og:description" content="在之前项目中我们想要读取MongoDB内的内容需要使用MongoDBTemplate来完成数据的CRUD，那如果我们想要通过RestController的形式获取MongoDB内的数据就更麻烦了，还需要自行去创建对应的控制器，然后使用MongoDBTemplate从MongoDB内读取出数据后返回给前端。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/4461954-d80b777ca38632ba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-04-22T02:33:44.478Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="第五十二章：基于SpringBoot2使用Rest访问MongoDB数据">
<meta name="twitter:description" content="在之前项目中我们想要读取MongoDB内的内容需要使用MongoDBTemplate来完成数据的CRUD，那如果我们想要通过RestController的形式获取MongoDB内的数据就更麻烦了，还需要自行去创建对应的控制器，然后使用MongoDBTemplate从MongoDB内读取出数据后返回给前端。">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/4461954-d80b777ca38632ba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
    

    
        <link rel="alternate" href="/" title="恒宇少年De成长之路" type="application/atom+xml" />
    

    

    <link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/libs/open-sans/styles.css">
    <link rel="stylesheet" href="/libs/source-code-pro/styles.css">

    <link rel="stylesheet" href="/css/style.css">

    <script src="/libs/jquery/2.1.3/jquery.min.js"></script>
    
    
        <link rel="stylesheet" href="/libs/lightgallery/css/lightgallery.min.css">
    
    
        <link rel="stylesheet" href="/libs/justified-gallery/justifiedGallery.min.css">
    
    
    
    


</head>

<body>
    <div id="container">
        <header id="header">
    <div id="header-main" class="header-inner">
        <div class="outer">
            <a href="/" id="logo">
                <i class="logo"></i>
                <span class="site-title">恒宇少年De成长之路</span>
            </a>
            <nav id="main-nav">
                
                    <a class="main-nav-link" href="/.">首页</a>
                
                    <a class="main-nav-link" href="/archives">时间轴</a>
                
                    <a class="main-nav-link" href="/categories">文章专题</a>
                
                    <a class="main-nav-link" href="/tags">标签云</a>
                
                    <a class="main-nav-link" href="/about">关于我</a>
                
            </nav>
            
                
                <nav id="sub-nav">
                    <div class="profile" id="profile-nav">
                        <a id="profile-anchor" href="javascript:;">
                            <img class="avatar" src="/css/images/avatar.png" />
                            <i class="fa fa-caret-down"></i>
                        </a>
                    </div>
                </nav>
            
            <div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
        <!--button type="submit" class="search-form-submit"></button-->
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="想要查找什么..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div>
        </div>
    </div>
    <div id="main-nav-mobile" class="header-sub header-inner">
        <table class="menu outer">
            <tr>
                
                    <td ><a  class="main-nav-link" href="/.">首页</a></td>
                
                    <td ><a  class="main-nav-link" href="/archives">时间轴</a></td>
                
                    <td ><a  class="main-nav-link" href="/categories">文章专题</a></td>
                
                    <td ><a  class="main-nav-link" href="/tags">标签云</a></td>
                
                    <td ><a  class="main-nav-link" href="/about">关于我</a></td>
                
                <td>
                    
    <div class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
    </div>

                </td>
            </tr>
        </table>
    </div>
</header>

        <div class="outer">
            
                

<aside id="profile">
    <div class="inner profile-inner">
        <div class="base-info profile-block">
            <img id="avatar" src="/css/images/avatar.png" />
            <h2 id="name">恒宇少年</h2>
            <h3 id="title">Java软件工程师 &amp; 程序猿</h3>
            <span id="location"><i class="fa fa-map-marker"></i>山东, 济南</span>
            <a id="follow" target="_blank" href="https://www.jianshu.com/u/092df3f77bca">关注我</a>
        </div>
        <div class="article-info profile-block">
            <div class="article-info-block">
                43
                <span>文章</span>
            </div>
            <div class="article-info-block">
                2
                <span>标签</span>
            </div>
        </div>
        
        <div class="profile-block social-links">
            <table>
                <tr>
                    
                    
                    <td>
                        <a href="http://github.com/ppoffice/hexo-theme-icarus" target="_blank" title="github" class=tooltip>
                            <i class="fa fa-github"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="/" target="_blank" title="twitter" class=tooltip>
                            <i class="fa fa-twitter"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="/" target="_blank" title="facebook" class=tooltip>
                            <i class="fa fa-facebook"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="/" target="_blank" title="dribbble" class=tooltip>
                            <i class="fa fa-dribbble"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="/" target="_blank" title="rss" class=tooltip>
                            <i class="fa fa-rss"></i>
                        </a>
                    </td>
                    
                </tr>
            </table>
        </div>
        
    </div>
</aside>

            
            <section id="main"><article id="post-spring-boot-chapter-51的副本" class="article article-type-post" itemscope itemprop="blogPost">
    <div class="article-inner">
        
        
            <header class="article-header">
                
    
        <h1 class="article-title" itemprop="name">
            第五十二章：基于SpringBoot2使用Rest访问MongoDB数据
        </h1>
    

                
                    <div class="article-meta">
                        
    <div class="article-date">
        <i class="fa fa-calendar"></i>
        <a href="/spring-boot-chapter-51的副本/">
            <time datetime="2018-04-21T16:00:00.000Z" itemprop="datePublished">2018-04-22</time>
        </a>
    </div>


                        
    <div class="article-category">
    	<i class="fa fa-folder"></i>
        <a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a>
    </div>

                        
    <div class="article-tag">
        <i class="fa fa-tag"></i>
        <a class="tag-link" href="/tags/SpringBoot/">SpringBoot</a>
    </div>

                    </div>
                
            </header>
        
        
        <div class="article-entry" itemprop="articleBody">
        
            
            <p>在之前项目中我们想要读取<code>MongoDB</code>内的内容需要使用<code>MongoDBTemplate</code>来完成数据的<code>CRUD</code>，那如果我们想要通过<code>RestController</code>的形式获取<code>MongoDB</code>内的数据就更麻烦了，还需要自行去创建对应的控制器，然后使用<code>MongoDBTemplate</code>从<code>MongoDB</code>内读取出数据后返回给前端。<br><a id="more"></a><br>在上一章节<a href="https://www.jianshu.com/p/2ec104e4ab39" target="_blank" rel="noopener">第五十一章：基于SpringBoot2 &amp; MongoDB完成自动化集成</a>我们讲到了<code>SpringBoot2</code>与<code>MongoDB</code>集成后怎么简单的操作数据，当然<code>Spring Data Xxx</code>家族方式的设计与<code>Spring Data JPA</code>一样，<code>Sring Data MongoDB</code>提供了一个<code>MongoRepository&lt;T,PK&gt;</code>接口来为继承该接口的子接口自动提供代理类完成数据操作实现。</p>
<h1 id="本章目标"><a href="#本章目标" class="headerlink" title="本章目标"></a>本章目标</h1><p>使用<code>Spring Data Rest</code>自动映射读取<code>MongoDB</code>内的数据，省去一系列繁琐的操作步骤。</p>
<h1 id="为你推荐"><a href="#为你推荐" class="headerlink" title="为你推荐"></a>为你推荐</h1><ol>
<li><a href="https://www.jianshu.com/p/2ec104e4ab39" target="_blank" rel="noopener">第五十一章：基于SpringBoot2 &amp; MongoDB完成自动化集成</a></li>
<li><a href="https://www.jianshu.com/p/4ccd4512700f" target="_blank" rel="noopener">第五十章：SpringBoot2.0新特性 - 岂止至今最简单redis缓存集成</a></li>
<li><a href="https://www.jianshu.com/p/77482f0b59e9" target="_blank" rel="noopener">第四十九章：SpringBoot2.0新特性 - 你get到WebMvcConfigurer两种配置方式了吗？</a></li>
<li><a href="https://www.jianshu.com/p/13fd9ff0648d" target="_blank" rel="noopener">第四十八章：SpringBoot2.0新特性 - RabbitMQ信任package设置</a> </li>
<li><a href="https://www.jianshu.com/p/056281e057b3" target="_blank" rel="noopener">第四十七章：SpringBoot2.0新特性 - Quartz自动化配置集成</a><h1 id="企业级核心技术学习专题"><a href="#企业级核心技术学习专题" class="headerlink" title="企业级核心技术学习专题"></a>企业级核心技术学习专题</h1>|专题     | 专题名称                                                              |专题描述    |<br>|:—–:    | :———————————————————————|:———–|<br>|001        | <a href="http://www.jianshu.com/c/3f69deddbed3" target="_blank" rel="noopener">Spring Boot 核心技术</a>         |讲解SpringBoot一些企业级层面的核心组件|<br>|002        | <a href="https://gitee.com/hengboy/spring-boot-chapter" target="_blank" rel="noopener">Spring Boot 核心技术章节源码</a>  |Spring Boot 核心技术简书每一篇文章码云对应源码|<br>|003        | <a href="http://www.jianshu.com/c/1faac22666e7" target="_blank" rel="noopener">Spring Cloud 核心技术</a>        |对Spring Cloud核心技术全面讲解|<br>|004        | <a href="https://gitee.com/hengboy/spring-cloud-chapter" target="_blank" rel="noopener">Spring Cloud 核心技术章节源码</a>  |Spring Cloud 核心技术简书每一篇文章对应源码|<br>|005        | <a href="http://www.jianshu.com/c/ab4789177827" target="_blank" rel="noopener">QueryDSL 核心技术</a>            |全面讲解QueryDSL核心技术以及基于SpringBoot整合SpringDataJPA|<br>|006        | <a href="http://www.jianshu.com/c/f1b269bb2fd6" target="_blank" rel="noopener">SpringDataJPA 核心技术</a>       |全面讲解SpringDataJPA核心技术|</li>
</ol>
<h1 id="构建项目"><a href="#构建项目" class="headerlink" title="构建项目"></a>构建项目</h1><p>使用<code>Idea</code>开发工具创建一个<code>SpringBoot</code>的项目，添加相应的依赖，pom.xml配置文件依赖内容如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependencies&gt;</span><br><span class="line">        &lt;!--mongodb依赖--&gt;</span><br><span class="line">        &lt;dependency&gt;</span><br><span class="line">            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;</span><br><span class="line">        &lt;/dependency&gt;</span><br><span class="line">        &lt;!--data rest依赖--&gt;</span><br><span class="line">        &lt;dependency&gt;</span><br><span class="line">            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt;</span><br><span class="line">        &lt;/dependency&gt;</span><br><span class="line">        &lt;dependency&gt;</span><br><span class="line">            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;</span><br><span class="line">            &lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">        &lt;/dependency&gt;</span><br><span class="line">        &lt;!--Lombok依赖--&gt;</span><br><span class="line">        &lt;dependency&gt;</span><br><span class="line">            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;lombok&lt;/artifactId&gt;</span><br><span class="line">        &lt;/dependency&gt;</span><br><span class="line">    &lt;/dependencies&gt;</span><br></pre></td></tr></table></figure></p>
<p>我们本章节的依赖比上一章多了一个<code>spring-boot-starter-data-rest</code>，通过这个依赖我们可以自动完成<code>RestController</code>的依赖配置，不需要再手动去创建控制器，因为我们通过一些简单的注解配置以及固定格式名称规则的方法就可以完成控制器的实现。</p>
<blockquote>
<p>因为本章的内容需要在上一章的基础上编写，所以我们直接把之前章节的相关的配置以及类都复制到本项目内，复制的内容有：<code>application.yml</code>、<code>Customer</code>、<code>CustomerRepository</code>。(源码位置：<a href="https://gitee.com/hengboy/spring-boot-chapter/tree/master/Chapter51" target="_blank" rel="noopener">第五十一章源码</a>)</p>
</blockquote>
<h3 id="改造CustomerRepository"><a href="#改造CustomerRepository" class="headerlink" title="改造CustomerRepository"></a>改造CustomerRepository</h3><p><code>spring-boot-starter-data-rest</code>会自动扫描添加<code>@RepositoryRestResource</code>注解的接口，自动将该接口映射为<code>一系列</code>可通过<code>rest</code>访问的请求路径，这里说到一系列，我们在测试的时候会讲到为什么说是<code>一系列！！！</code>。<br>既然需要添加注解，那么我们就打开<code>CustomerRepository</code>接口，对应为它添加上如下注解内容：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@RepositoryRestResource(collectionResourceRel = &quot;customer&quot;, path = &quot;customer&quot;)</span><br><span class="line">public interface CustomerRepository extends MongoRepository&lt;Customer, String&gt; &#123;</span><br><span class="line">//....省略</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>注解内需要提供两个参数，<br><code>collectionResourceRel</code>：该参数配置映射<code>MongoDB</code>内的<code>Collection</code>名称。<br><code>path</code>：该参数配置映射完成<code>rest</code>后访问的路径前缀。</p>
<h1 id="运行测试"><a href="#运行测试" class="headerlink" title="运行测试"></a>运行测试</h1><p>我们先来简单的运行测试下是否可以通过我们配置的<code>path</code>路径实现访问内容，启动项目时我们可以看到控制台的输出内容：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Mapped &quot;&#123;[/&#123;repository&#125;/search],methods=[GET]</span><br><span class="line">Mapped &quot;&#123;[/&#123;repository&#125;/search/&#123;search&#125;],methods=[GET]</span><br><span class="line">Mapped &quot;&#123;[/&#123;repository&#125;/&#123;id&#125;/&#123;property&#125;],methods=[GET]</span><br><span class="line">Mapped &quot;&#123;[/&#123;repository&#125;],methods=[GET]</span><br><span class="line">....</span><br></pre></td></tr></table></figure></p>
<p>我们配置一个<code>@RepositoryRestResource</code>注解的接口就会根据<code>rest</code>内置的一系列的条件生成对应的请求，这也是我们在之前说到的<code>一系列</code>请求路径的地方，我们先来访问下映射<code>/{repository}</code>的路径。</p>
<h4 id="测试-repository-映射路径"><a href="#测试-repository-映射路径" class="headerlink" title="测试 /{repository} 映射路径"></a>测试 /{repository} 映射路径</h4><blockquote>
<p>你如果使用<code>Windows</code>系统直接打开浏览器输出地址就可以看到返回的内容，如果你使用<code>Linux</code>或者<code>OS X</code>系统可以在<code>Terminal</code>使用<code>curl</code>命令查看返回内容。</p>
</blockquote>
<p>我们访问：<a href="http://localhost:8080/customer" target="_blank" rel="noopener">http://localhost:8080/customer</a>，路径查看返回的内容：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">➜  ~ curl http://localhost:8080/customer</span><br><span class="line">&#123;</span><br><span class="line">  &quot;_embedded&quot; : &#123;</span><br><span class="line">    &quot;customer&quot; : [ &#123;</span><br><span class="line">      &quot;firstName&quot; : &quot;恒宇&quot;,</span><br><span class="line">      &quot;lastName&quot; : &quot;少年&quot;,</span><br><span class="line">      &quot;_links&quot; : &#123;</span><br><span class="line">        &quot;self&quot; : &#123;</span><br><span class="line">          &quot;href&quot; : &quot;http://localhost:8080/customer/5adbec9ceb89f105acd90cec&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;customer&quot; : &#123;</span><br><span class="line">          &quot;href&quot; : &quot;http://localhost:8080/customer/5adbec9ceb89f105acd90cec&quot;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; ]</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;_links&quot; : &#123;</span><br><span class="line">    &quot;self&quot; : &#123;</span><br><span class="line">      &quot;href&quot; : &quot;http://localhost:8080/customer&#123;?page,size,sort&#125;&quot;,</span><br><span class="line">      &quot;templated&quot; : true</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;profile&quot; : &#123;</span><br><span class="line">      &quot;href&quot; : &quot;http://localhost:8080/profile/customer&quot;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;page&quot; : &#123;</span><br><span class="line">    &quot;size&quot; : 20,</span><br><span class="line">    &quot;totalElements&quot; : 1,</span><br><span class="line">    &quot;totalPages&quot; : 1,</span><br><span class="line">    &quot;number&quot; : 0</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>通过这个地址我们可以读取出<code>@RepositoryRestResource注解</code>配置的<code>collectionResourceRel</code>对应的 <code>MongoDB.collection</code>集合内的数据，我们发现不仅读取出来了数据而且还为我们提供了<code>分页的信息</code>，这可是很贴心的地方啊，默认读取<code>第1页</code>，<code>每页20条</code>数据。</p>
<h4 id="测试-repository-id-映射路径"><a href="#测试-repository-id-映射路径" class="headerlink" title="测试 /{repository}/{id} 映射路径"></a>测试 /{repository}/{id} 映射路径</h4><p>我们访问<a href="http://localhost:8080/customer/5adbec9ceb89f105acd90cec" target="_blank" rel="noopener">http://localhost:8080/customer/5adbec9ceb89f105acd90cec</a>（注意：这里的id是你本地生成的，这个id是我本地生成，直接访问会出现404）如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">➜  ~ curl http://localhost:8080/customer/5adbec9ceb89f105acd90cec</span><br><span class="line">&#123;</span><br><span class="line">  &quot;firstName&quot; : &quot;恒宇&quot;,</span><br><span class="line">  &quot;lastName&quot; : &quot;少年&quot;,</span><br><span class="line">  &quot;_links&quot; : &#123;</span><br><span class="line">    &quot;self&quot; : &#123;</span><br><span class="line">      &quot;href&quot; : &quot;http://localhost:8080/customer/5adbec9ceb89f105acd90cec&quot;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;customer&quot; : &#123;</span><br><span class="line">      &quot;href&quot; : &quot;http://localhost:8080/customer/5adbec9ceb89f105acd90cec&quot;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>根据返回的内容看到是能够访问根据<code>id</code>查询的数据内容的。</p>
<h4 id="测试-repository-search-search-映射路径"><a href="#测试-repository-search-search-映射路径" class="headerlink" title="测试 /{repository}/search/{search} 映射路径"></a>测试 /{repository}/search/{search} 映射路径</h4><p>这个映射的配置是专门为我们自定义方法准备的，自定义方法的规则与<code>SpringDataJPA</code>的方法名称规则一样，当我们在接口创建<code>findByXxx</code>方法时<code>Idea</code>会自动为我们提示相应的内容，下面我们就创建两个不同的查询方法，如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 更加名字查询数据</span><br><span class="line"> *</span><br><span class="line"> * @param firstName 名字</span><br><span class="line"> * @return</span><br><span class="line"> */</span><br><span class="line">List&lt;Customer&gt; findByFirstName(@Param(&quot;firstName&quot;) String firstName);</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 根据姓氏查询出最靠前的一条数据</span><br><span class="line"> *</span><br><span class="line"> * @param lastName 姓氏</span><br><span class="line"> * @return</span><br><span class="line"> */</span><br><span class="line">Customer findTopByLastName(@Param(&quot;lastName&quot;) String lastName);</span><br></pre></td></tr></table></figure></p>
<p>下面我们重启下项目访问路径<a href="http://localhost:8080/customer/search/findByFirstName?firstName=%E6%81%92%E5%AE%87" target="_blank" rel="noopener">http://localhost:8080/customer/search/findByFirstName?firstName=恒宇</a>可以看到返回内容：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">➜  ~ curl http://localhost:8080/customer/search/findByFirstName\?firstName\=%E6%81%92%E5%AE%87</span><br><span class="line">&#123;</span><br><span class="line">  &quot;_embedded&quot; : &#123;</span><br><span class="line">    &quot;customer&quot; : [ &#123;</span><br><span class="line">      &quot;firstName&quot; : &quot;恒宇&quot;,</span><br><span class="line">      &quot;lastName&quot; : &quot;少年&quot;,</span><br><span class="line">      &quot;_links&quot; : &#123;</span><br><span class="line">        &quot;self&quot; : &#123;</span><br><span class="line">          &quot;href&quot; : &quot;http://localhost:8080/customer/5adbec9ceb89f105acd90cec&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;customer&quot; : &#123;</span><br><span class="line">          &quot;href&quot; : &quot;http://localhost:8080/customer/5adbec9ceb89f105acd90cec&quot;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; ]</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;_links&quot; : &#123;</span><br><span class="line">    &quot;self&quot; : &#123;</span><br><span class="line">      &quot;href&quot; : &quot;http://localhost:8080/customer/search/findByFirstName?firstName=%E6%81%92%E5%AE%87&quot;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>自动的根据我们的配置的方法查询出了对应的数据，自动过滤了对应的数据，不过这个是没有分页的。<br>同样另外一个自定义方法的请求<a href="http://localhost:8080/customer/search/findTopByLastName?lastName=%E5%B0%91%E5%B9%B4" target="_blank" rel="noopener">http://localhost:8080/customer/search/findTopByLastName?lastName=少年</a>，也是一样的可以对应的获取过滤后的数据。</p>
<blockquote>
<p>注意：@Param注解内的参数名称要与<code>Customer</code>内的属性对应。</p>
</blockquote>
<p>如果你想查看配置的全部自定义的方法，访问：<a href="http://localhost:8080/customer/search" target="_blank" rel="noopener">http://localhost:8080/customer/search</a>，如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">➜  ~ curl http://localhost:8080/customer/search                                               </span><br><span class="line">&#123;</span><br><span class="line">  &quot;_links&quot; : &#123;</span><br><span class="line">    &quot;findByFirstName&quot; : &#123;</span><br><span class="line">      &quot;href&quot; : &quot;http://localhost:8080/customer/search/findByFirstName&#123;?firstName&#125;&quot;,</span><br><span class="line">      &quot;templated&quot; : true</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;findTopByLastName&quot; : &#123;</span><br><span class="line">      &quot;href&quot; : &quot;http://localhost:8080/customer/search/findTopByLastName&#123;?lastName&#125;&quot;,</span><br><span class="line">      &quot;templated&quot; : true</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;self&quot; : &#123;</span><br><span class="line">      &quot;href&quot; : &quot;http://localhost:8080/customer/search&quot;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>本章内容主要是围绕着<code>spring-boot-starter-data-rest</code>这个依赖进行的，这个依赖帮助我们完成了日常编码中一些重复的工作，而且很智能的提供了一些映射，更方便我们进行查询数据。</p>
<p>本章源码已经上传到码云：<br>SpringBoot配套源码地址：<a href="https://gitee.com/hengboy/spring-boot-chapter" target="_blank" rel="noopener">https://gitee.com/hengboy/spring-boot-chapter</a><br>SpringCloud配套源码地址：<a href="https://gitee.com/hengboy/spring-cloud-chapter" target="_blank" rel="noopener">https://gitee.com/hengboy/spring-cloud-chapter</a><br>SpringBoot相关系列文章请访问：<a href="http://www.jianshu.com/p/9a08417e4e84" target="_blank" rel="noopener">目录：SpringBoot学习目录</a><br>QueryDSL相关系列文章请访问：<a href="http://www.jianshu.com/p/99a5ec5c3bd5" target="_blank" rel="noopener">QueryDSL通用查询框架学习目录</a><br>SpringDataJPA相关系列文章请访问：<a href="http://www.jianshu.com/p/615ed9c1fe84" target="_blank" rel="noopener">目录：SpringDataJPA学习目录</a>，感谢阅读！<br>欢迎加入QQ技术交流群，共同进步。<br><img src="http://upload-images.jianshu.io/upload_images/4461954-d80b777ca38632ba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="QQ技术交流群1"></p>

        
        </div>
        <footer class="article-footer">
            <div class="share-container">


    <div class="bdsharebuttonbox">
    <a href="#" class="bds_more" data-cmd="more">分享到：</a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间">QQ空间</a>
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博">新浪微博</a>
    <a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博">腾讯微博</a>
    <a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网">人人网</a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信">微信</a>
</div>
<script>
window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{"bdSize":16}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
</script>
<style>
    .bdshare_popup_box {
        border-radius: 4px;
        border: #e1e1e1 solid 1px;
    }
    .bdshare-button-style0-16 a,
    .bdshare-button-style0-16 .bds_more {
        padding-left: 20px;
        margin: 6px 10px 6px 0;
    }
    .bdshare_dialog_list a,
    .bdshare_popup_list a,
    .bdshare_popup_bottom a {
        font-family: 'Microsoft Yahei';
    }
    .bdshare_popup_top {
        display: none;
    }
    .bdshare_popup_bottom {
        height: auto;
        padding: 5px;
    }
</style>


</div>

            
    

        </footer>
    </div>
    
        
<nav id="article-nav">
    
    
        <a href="/spring-boot-chapter-51/" id="article-nav-older" class="article-nav-link-wrap">
            <strong class="article-nav-caption">下一篇</strong>
            <div class="article-nav-title">第五十一章：基于SpringBoot2 &amp; MongoDB完成自动化集成</div>
        </a>
    
</nav>


    
</article>


    
    

</section>
            
			
				
<aside id="sidebar">
   
        
    <div class="widget-wrap">
        <h3 class="widget-title">最新文章</h3>
        <div class="widget">
            <ul id="recent-post" class="no-thumbnail">
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a></p>
                            <p class="item-title"><a href="/spring-boot-chapter-51的副本/" class="title">第五十二章：基于SpringBoot2使用Rest访问MongoDB数据</a></p>
                            <p class="item-date"><time datetime="2018-04-21T16:00:00.000Z" itemprop="datePublished">2018-04-22</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a></p>
                            <p class="item-title"><a href="/spring-boot-chapter-51/" class="title">第五十一章：基于SpringBoot2 &amp; MongoDB完成自动化集成</a></p>
                            <p class="item-date"><time datetime="2018-04-15T16:00:00.000Z" itemprop="datePublished">2018-04-16</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a></p>
                            <p class="item-title"><a href="/spring-boot-chapter-50/" class="title">第五十章：SpringBoot2.0新特性 - 岂止至今最简单redis缓存集成</a></p>
                            <p class="item-date"><time datetime="2018-04-14T16:00:00.000Z" itemprop="datePublished">2018-04-15</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a></p>
                            <p class="item-title"><a href="/spring-boot-chapter-49/" class="title">第四十九章：SpringBoot2.0新特性 - 你get到WebMvcConfigurer两种配置方式了吗？</a></p>
                            <p class="item-date"><time datetime="2018-03-16T16:00:00.000Z" itemprop="datePublished">2018-03-17</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a></p>
                            <p class="item-title"><a href="/spring-boot-chapter-48/" class="title">第四十八章：SpringBoot2.0新特性 - RabbitMQ信任package设置</a></p>
                            <p class="item-date"><time datetime="2018-03-12T16:00:00.000Z" itemprop="datePublished">2018-03-13</time></p>
                        </div>
                    </li>
                
            </ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">分类</h3>
        <div class="widget">
            <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/QueryDSL-核心技术/">QueryDSL 核心技术</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a><span class="category-list-count">36</span></li></ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">归档</h3>
        <div class="widget">
            <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">四月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">三月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">一月 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">十二月 2017</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">十一月 2017</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">十月 2017</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">九月 2017</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/">八月 2017</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">七月 2017</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">四月 2017</a><span class="archive-list-count">5</span></li></ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">标签</h3>
        <div class="widget">
            <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/QueryDSL/">QueryDSL</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SpringBoot/">SpringBoot</a><span class="tag-list-count">43</span></li></ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">标签云</h3>
        <div class="widget tagcloud">
            <a href="/tags/QueryDSL/" style="font-size: 10px;">QueryDSL</a> <a href="/tags/SpringBoot/" style="font-size: 20px;">SpringBoot</a>
        </div>
    </div>

    
        
    <div class="widget-wrap widget-list">
        <h3 class="widget-title">链接</h3>
        <div class="widget">
            <ul>
                
                    <li>
     				<a href="http://hexo.io" target="_blank">Hexo</a>
					</li>
                
            </ul>
        </div>
    </div>


    
    <div id="toTop" class="fa fa-angle-up"></div>
</aside>

				
        </div>
        <!--引入不蒜子-->
<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<footer id="footer">
    <div class="outer">
        <div id="footer-info" class="inner">
            <span  id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
            &copy; 2017 - 2018 恒宇少年 - 版权所有<br>
        </div>
    </div>
</footer>

        


    
        <script src="/libs/lightgallery/js/lightgallery.min.js"></script>
        <script src="/libs/lightgallery/js/lg-thumbnail.min.js"></script>
        <script src="/libs/lightgallery/js/lg-pager.min.js"></script>
        <script src="/libs/lightgallery/js/lg-autoplay.min.js"></script>
        <script src="/libs/lightgallery/js/lg-fullscreen.min.js"></script>
        <script src="/libs/lightgallery/js/lg-zoom.min.js"></script>
        <script src="/libs/lightgallery/js/lg-hash.min.js"></script>
        <script src="/libs/lightgallery/js/lg-share.min.js"></script>
        <script src="/libs/lightgallery/js/lg-video.min.js"></script>
    
    
        <script src="/libs/justified-gallery/jquery.justifiedGallery.min.js"></script>
    
    



<!-- Custom Scripts -->
<script src="/js/main.js"></script>

    </div>
</body>
</html>